package com.csci;

public class DFSAlgorithm {

	RandomGraph rGraph;
	int time;
	VertexDFS[] vertex;
	
	public DFSAlgorithm(RandomGraph rGraph){
		this.rGraph = rGraph;
	}
	
	public DFSAlgorithm(){
		
	}
	
	public void DFS() {
		for(int i = 0; i < vertex.length; i++) {
			vertex[i].setColor(STATE.white);
			vertex[i].setPred((int)Double.POSITIVE_INFINITY);
		}
		time = 0;
		for(int i = 0; i < vertex.length; i++){
			if(vertex[i].getColor() == STATE.white){
				DFS_Visit(vertex[i]);
			}
		}
	}
	
	public void DFS_Visit(VertexDFS u){
		time++;
		u.setStartTime(time);
		System.out.println("The start time for vertex " + u.id + " is " + u.getStartTime());
		u.setColor(STATE.gray);
		for(int edge = 0; edge < u.getEdgeList().edges.size(); edge++){
			VertexDFS v = u.getEdgeList().edges.get(edge);
			if(v.getColor() == STATE.white){
				v.setPred(u.getId());
				DFS_Visit(v);
			}
		}
		u.setColor(STATE.black);
		time++;
		u.setEndTime(time);
		System.out.println("The start time for vertex " + u.id + " is " + u.getEndTime());
	}
	
	public void initialize() {
		/*vertex = new VertexDFS[6];
		
		vertex[0] = new VertexDFS();
		vertex[0].id = 'u';
	
		vertex[1] = new VertexDFS();
		vertex[1].id = 'v';
		
		vertex[2] = new VertexDFS();
		vertex[2].id = 'w';
		
		vertex[3] = new VertexDFS();
		vertex[3].id = 'x';
		
		vertex[4] = new VertexDFS();
		vertex[4].id = 'y';
		
		vertex[5] = new VertexDFS();
		vertex[5].id = 'z';
		
		
		vertex[0].edgeList.edges.add(vertex[1]);
		vertex[0].edgeList.edges.add(vertex[3]);
		
		vertex[1].edgeList.edges.add(vertex[4]);
		
		vertex[2].edgeList.edges.add(vertex[4]);
		vertex[2].edgeList.edges.add(vertex[5]);
		
		vertex[3].edgeList.edges.add(vertex[1]);
		
		vertex[4].edgeList.edges.add(vertex[3]);
		
		vertex[5].edgeList.edges.add(vertex[5]);
		*/
		
		vertex = new VertexDFS[8];
		
		vertex[0] = new VertexDFS();
		vertex[0].id = '0';
		
		vertex[1] = new VertexDFS();
		vertex[1].id = '1';
		
		vertex[2] = new VertexDFS();
		vertex[2].id = '2';
		
		vertex[3] = new VertexDFS();
		vertex[3].id = '3';
		
		vertex[4] = new VertexDFS();
		vertex[4].id = '4';
		
		vertex[5] = new VertexDFS();
		vertex[5].id = '5';
		
		vertex[6] = new VertexDFS();
		vertex[6].id = '6';
		
		vertex[7] = new VertexDFS();
		vertex[7].id = '7';
		
		
		vertex[0].edgeList.edges.add(vertex[1]);
		vertex[0].edgeList.edges.add(vertex[3]);
		vertex[0].edgeList.edges.add(vertex[7]);
		
		vertex[1].edgeList.edges.add(vertex[2]);
		
		vertex[2].edgeList.edges.add(vertex[0]);
		
		vertex[3].edgeList.edges.add(vertex[7]);
		
		vertex[4].edgeList.edges.add(vertex[1]);
		vertex[4].edgeList.edges.add(vertex[5]);
		
		vertex[5].edgeList.edges.add(vertex[1]);
		vertex[5].edgeList.edges.add(vertex[2]);
		vertex[5].edgeList.edges.add(vertex[4]);
		
		vertex[6].edgeList.edges.add(vertex[3]);
		
		vertex[7].edgeList.edges.add(vertex[6]);
	}
	
	public void displayGraph() {
		for(int i = 0; i < vertex.length; i++){
			int j = 0;
			System.out.print("The vertex " + vertex[i].id + " :");
			while(j < vertex[i].edgeList.edges.size()){
				System.out.print(vertex[i].edgeList.edges.get(j).id + ", ");
				j++;
			}
			System.out.println();
		}
	}
}
